package com.gdssoft.pdaweb.service.impl;

import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.gdssoft.pdaweb.model.CrmWorkOrder;
import com.gdssoft.pdaweb.model.am.PdaAmMeter;
import com.gdssoft.pdaweb.model.am.PdaAmMeterList;
import com.gdssoft.pdaweb.model.water.PdaWaterList;
import com.gdssoft.pdaweb.model.water.PdaWaterMeter;
import com.gdssoft.pdaweb.service.CrmService;
import synch.*;

public class CrmServiceImpl implements CrmService {

	private transient final Log logger = LogFactory.getLog(getClass());
	
	@Override
	public boolean syncWordOrderStatus(CrmWorkOrder crmWorkOrder) {	
		
		logger.info("syncWordOrderStatus executed");
		
		boolean result = false;
		
		// 活动编号，即：客服单号
		String activityUID = crmWorkOrder.getActivityUID();
		// 状态
		String workStatus = transfrtWorkStatus(crmWorkOrder.getWorkStatus());
		// 完成时间
		Date plannedCompletion = crmWorkOrder.getPlannedCompletion();
		// 备注
		String comment = crmWorkOrder.getComment();

		logger.info("参数：" + activityUID + " | " + workStatus + " | " + 
				plannedCompletion + " | " + comment);
		
		try {
			result = SynchCustomWorkOrderStatus.synchCustomWorkOrderStatus(
					activityUID, workStatus, plannedCompletion, comment);
		} catch (Exception ex) {
			logger.error("同步工单状态失败，", ex);
		}
		return result;
	}
    
	@Override
	public boolean pushWaterMeter(PdaWaterMeter pdaWaterMeter) {
		
		logger.info("pushWaterMeter executed");
		
		boolean result = false;
		
		// 业主编号
		String clientId = pdaWaterMeter.getClientId();
		
		// 水表号
		String meterId = pdaWaterMeter.getAmId();
		
		String type = "水表";
		
		// 使用性质，“商业”、“住宅”、“临时”、“共用”、“事业单位”、“其他”
		String meterKind = transferMeterkind(pdaWaterMeter.getMeterKind());
		
		// 是否室内
		boolean inDoor = !"1".equals(pdaWaterMeter.getIndoor());
		
		// 是否收费 
		boolean isCharge = !"1".equals(pdaWaterMeter.getIsCharge());
		
		// 状态，值为“申请中”、“活动中”、“已报废”
		String status = transfrtMeterStatus(pdaWaterMeter.getType());
		
		// 倍率，值为整数 正常为1 
		float timesNum = new Float(pdaWaterMeter.getTimesNum()).floatValue();
		
		// 父表编号
		String upId = pdaWaterMeter.getUpId();
		
		logger.info("参数：" + clientId + " | " + meterId + " | " + type + " | " + 
				meterKind + " | " + inDoor + " | " + isCharge + " | " + 
				status + " | " + timesNum + " | " + upId);
		try{
			result = SynchAssetMessge.SynchAsset(clientId, meterId, type, 
					meterKind, inDoor, isCharge, status, timesNum, upId);
		}
		catch(Exception ex){
			logger.error("同步水表失败，" ,ex);
		}
		return result;
	}
	
	@Override
	public boolean pushWaterRecord(PdaWaterList pdaWaterList) {
		
		logger.info("pushWaterRecord executed");
		
		boolean result = false;
		
		//表编号，系统唯一
		String meterId = pdaWaterList.getAmId(); 		
		//抄表日期
		Date recordTime = pdaWaterList.getRecordTime();
		
		//本期读数
		int degree = pdaWaterList.getDegree().intValue(); 	
		if (degree == -1) degree = 0;
		
		//上期读数，上期抄表的读数
		int lastDegree = pdaWaterList.getLastDegree().intValue();
		if (lastDegree == -1) lastDegree = 0;
		
		//本期用量
		int usage = pdaWaterList.getUsage().intValue();
		if (usage == -1) usage = 0;
		
		//上月用量暂时无法取得
		int lastUsage = 0; 
		if (lastUsage == -1) lastUsage = 0;
		
		//上月修正量，无修正时为0
		int fixedUsage = pdaWaterList.getFixedUsage().intValue();
		if (fixedUsage == -1) fixedUsage = 0;
		
		//备注信息，可为空字符串
		String comments = pdaWaterList.getComments();
		
		logger.info("参数：" + meterId + " | " + recordTime + " | " + 
				degree + " | " + lastDegree + " | " + usage + " | " + 
				lastUsage + " | " + fixedUsage + " | " + comments);
		
		try{
			result = SynchAssetReading.synchAssetReading(
				meterId, recordTime, degree, lastDegree, 
				usage, lastUsage, fixedUsage, comments);
		}
		catch(Exception ex){
			logger.error("同步水表读数失败，" ,ex);
		}
		return result;
	}

	@Override
	public boolean pushAmMeter(PdaAmMeter pdaAmMeter) {
		
		logger.info("pushAmMeter executed");
		
		boolean result = false;
		
		// 业主编号
		String clientId = pdaAmMeter.getClientId();
		
		// 水表号
		String meterId = pdaAmMeter.getAmId();
		
		String type = "电表";
		
		// 使用性质，“商业”、“住宅”、“临时”、“共用”、“事业单位”、“其他”
		String meterKind = transferMeterkind(pdaAmMeter.getMeterKind());
		
		// 是否室内
		boolean inDoor = !"1".equals(pdaAmMeter.getIndoor());
		
		// 是否收费 
		boolean isCharge = !"1".equals(pdaAmMeter.getIsCharge());
		
		// 状态，值为“申请中”、“活动中”、“已报废”
		String status = transfrtMeterStatus(pdaAmMeter.getType());
		
		// 倍率，值为整数 正常为1 
		float timesNum = new Float(pdaAmMeter.getTimesNum()).floatValue();
		
		// 父表编号
		String upId = pdaAmMeter.getUpId();
		
		logger.info("参数：" + clientId + " | " + meterId + " | " + type + " | " + 
				meterKind + " | " + inDoor + " | " + isCharge + " | " + 
				status + " | " + timesNum + " | " + upId);
		
		try{
			result = SynchAssetMessge.SynchAsset(clientId, meterId, type, 
					meterKind, inDoor, isCharge, status, timesNum, upId);
		}
		catch(Exception ex){
			logger.error("同步电表失败，" ,ex);
		}
		return result;
	}

	@Override
	public boolean pushAmRecord(PdaAmMeterList pdaAmMeterList) {
		
		logger.info("pushAmRecord executed");
		
		boolean result = false;
		
		//表编号，系统唯一
		String meterId = pdaAmMeterList.getAmId(); 		
		//抄表日期
		Date recordTime = pdaAmMeterList.getRecordtime();
		
		//本期读数
		int degree = pdaAmMeterList.getDegree().intValue(); 	
		if (degree == -1) degree = 0;
		
		//上期读数，上期抄表的读数
		int lastDegree = pdaAmMeterList.getLastDegree().intValue();
		if (lastDegree == -1) lastDegree = 0;
		
		//本期用量
		int usage = pdaAmMeterList.getUsage().intValue();
		if (usage == -1) usage = 0;
		
		//上月用量暂时无法取得
		int lastUsage = 0; 
		if (lastUsage == -1) lastUsage = 0;
		
		//上月修正量，无修正时为0
		int fixedUsage = pdaAmMeterList.getFixedUsage().intValue();
		if (fixedUsage == -1) fixedUsage = 0;
		
		//备注信息，可为空字符串
		String comments = pdaAmMeterList.getComments();
		
		logger.info("参数：" + meterId + " | " + recordTime + " | " + 
				degree + " | " + lastDegree + " | " + usage + " | " + 
				lastUsage + " | " + fixedUsage + " | " + comments);
		
		try{
			result = SynchAssetReading.synchAssetReading(
					meterId, recordTime, degree, lastDegree, 
					usage, lastUsage, fixedUsage, comments);
		}
		catch(Exception ex){
			logger.error("同步电表读数失败，" ,ex);
		}
		return result;
	}
	
	/*
	public static void main(String[] args) {
		CrmServiceImpl c = new CrmServiceImpl();
		System.out.println(c.transfrtWorkStatus("1"));
		System.out.println(c.transfrtWorkStatus("2"));
		System.out.println(c.transfrtWorkStatus("3"));
		System.out.println(c.transfrtWorkStatus("9"));
		System.out.println(c.transfrtWorkStatus("10"));
	}*/

	
    private String transfrtWorkStatus(String num){
	   if (num.equals("1")) {
		   return "未处理";
	   } else if(num.equals("2")) {
		   return "转单";
	   } else if (num.equals("3")) {
		   return "报修";
	   } else if (num.equals("9")) {
		   return "完成";
	   } else {
			logger.warn("客服工单状态：\"" + num + "\"，无法识别~~");
			return null;
		}
    }
    private String transfrtMeterStatus(String code){
    	//"申请中”、“活动中”、“已报废
	   if (code.equals("1")) {
		   return "申请中";
	   } else if(code.equals("2")) {
		   return "使用中";
	   } else if (code.equals("3")) {
		   return "已报废";
	   } else if (code.equals("4")) {
		   return "已停用";
	   } else {
			return code;
		}
    }
	private String  transferMeterkind(String code ){
		if("1".equals(code)){
			return "商业";
		}
		if("2".equals(code)){
			return "公共";
		}
		if("3".equals(code)){
			return "住宅";
		}
		if("4".equals(code)){
			return "临时";
		}
		if("5".equals(code)){
			return "事业单位";
		}
		else{
		return "其他";
		}
	}
}
